package ar.com.android.alco.providers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ar.com.android.alco.dbHelpers.SQLALCOHelper;
import ar.com.android.alco.entities.ControlPeso;

public class ControlPesoProvider {
	
	private static final String DB_COLUMN_0_NAME = "idControlPeso";
	private static final String DB_COLUMN_1_NAME = "fechaControl";
	private static final String DB_COLUMN_2_NAME = "peso";
	private static final String DB_COLUMN_3_NAME = "creditosDisp";

	private SQLiteDatabase database;
	private SQLALCOHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME, DB_COLUMN_3_NAME};
	
	public ControlPesoProvider(Context context) {
		dbHelper = SQLALCOHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertControlPeso(ControlPeso controlPeso)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_0_NAME, controlPeso.getIdControlPeso());
		contentValues.put(DB_COLUMN_1_NAME, controlPeso.getFechaControl().toString());
		contentValues.put(DB_COLUMN_2_NAME, controlPeso.getPeso());
		contentValues.put(DB_COLUMN_3_NAME, controlPeso.getCreditosDisp());
		
		long insertId = database.insert(SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeControlPeso(Integer idControlPeso)
	{
		int result = database.delete(SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idControlPeso, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeControlPeso()
	{
		int result = database.delete(SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
		
	public long updateControlPeso(ControlPeso controlPeso)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, controlPeso.getFechaControl().toString());
		contentValues.put(DB_COLUMN_2_NAME, controlPeso.getPeso());
		contentValues.put(DB_COLUMN_3_NAME, controlPeso.getCreditosDisp());
		
		return database.update(SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + controlPeso.getIdControlPeso(), null);
	}
	
	public ControlPeso getControlPeso(Integer idControlPeso)
	{
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			Cursor cursor = database.query(SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME, allColumns, DB_COLUMN_0_NAME + " = " + idControlPeso, null, null, null, null);
			cursor.moveToFirst();
			
			ControlPeso controlPeso = new ControlPeso();
			controlPeso.setIdControlPeso(cursor.getInt(0));
			controlPeso.setFechaControl(sdf.parse(cursor.getString(1)));
			controlPeso.setPeso(cursor.getDouble(2));
			controlPeso.setCreditosDisp(cursor.getDouble(3));
			controlPeso.setSelected(false);
			
			cursor.close();
			return controlPeso;
	
		}
		catch (Exception e) {
			return null;
		}		
	}
	
	public List<ControlPeso> getControlesPeso() 
	{
		try
		{
			List<ControlPeso> lstControlPeso = new ArrayList<ControlPeso>();
			
			Cursor cursor = database.query(SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME, allColumns, null, null, null, null, null);
			cursor.moveToFirst();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			while (!cursor.isAfterLast()) {
				ControlPeso controlPeso = new ControlPeso();
				controlPeso.setIdControlPeso(cursor.getInt(0));
				controlPeso.setFechaControl(sdf.parse(cursor.getString(1)));
				controlPeso.setPeso(cursor.getDouble(2));
				controlPeso.setCreditosDisp(cursor.getDouble(3));
				controlPeso.setSelected(false);
				
				lstControlPeso.add(controlPeso);
				cursor.moveToNext();
			}
			cursor.close();
			return lstControlPeso;
		}
		catch (Exception e) {
			return new ArrayList<ControlPeso>();
		}
	}
	
	public Boolean validAddDate(Date dDate) {
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			String strSQL = "SELECT MAX(" + DB_COLUMN_1_NAME + ") AS fechaControl FROM " + SQLALCOHelper.DB_CONTROL_PESO_TABLE_NAME;
			Cursor cursor = database.rawQuery(strSQL, null);
			cursor.moveToFirst();

			if(cursor.getString(0) == null)
				return Boolean.TRUE;
			
			if(sdf.parse(cursor.getString(0)).compareTo(dDate) < 0) 
				return Boolean.TRUE;
			else
				return Boolean.FALSE;
		}
		catch (Exception e) {
			return Boolean.FALSE;
		}
	}
}
